<?php

use Drupal\shusy_errand\Entity\ShusyErrand;
use Drupal\shusy_errand\Entity\ShusyErrandOrder;

function shusy_errand_shusy_errand_entity_form($form, &$form_state, ShusyErrand $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_errand') . '/shusy_errand.forms.inc';
  $form_state[ShusyErrand::class] = $e;

  $form['city_id'] = [
    '#type' => 'select',
    '#title' => t('所属城市'),
    '#options' => shusy_city_options(),
    '#required' => TRUE,
    '#default_value' => $e->city_id,
  ];
  $form['errand_id'] = [
    '#type' => 'textfield',
    '#title' => t('所属快达团队'),
    '#required' => TRUE,
    '#default_value' => $e->errand_id,
    '#element_validate' => ['element_validate_integer'],
  ];
  $form['is_admin'] = [
    '#type' => 'checkbox',
    '#title' => t('团队管理'),
    '#default_value' => $e->is_admin,
  ];
  $form['team'] = [
    '#type' => 'textfield',
    '#title' => t('团队名'),
    '#required' => TRUE,
    '#default_value' => $e->team,
  ];
  $form['city'] = [
    '#type' => 'textfield',
    '#title' => t('区域'),
    '#required' => TRUE,
    '#default_value' => $e->city,
  ];
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('姓名'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['phone'] = [
    '#type' => 'textfield',
    '#title' => t('联系电话'),
    '#required' => TRUE,
    '#default_value' => $e->phone,
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
  ];
  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_errand_shusy_errand_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyErrand::class];
  if ($e instanceof ShusyErrand) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_errand_shusy_errand_delete_form($form, &$form_state, ShusyErrand $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_errand') . '/shusy_errand.forms.inc';
  $form_state[ShusyErrand::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_errand/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_errand_shusy_errand_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyErrand::class];
  if ($e instanceof ShusyErrand) {
    $e->delete();
  }
}

function shusy_errand_shusy_errand_order_entity_form($form, &$form_state, ShusyErrandOrder $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_errand') . '/shusy_errand.forms.inc';
  $form_state[ShusyErrandOrder::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_errand_shusy_errand_order_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyErrandOrder::class];
  if ($e instanceof ShusyErrandOrder) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_errand_shusy_errand_order_delete_form($form, &$form_state, ShusyErrandOrder $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_errand') . '/shusy_errand.forms.inc';
  $form_state[ShusyErrandOrder::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_errand_order/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_errand_shusy_errand_order_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyErrandOrder::class];
  if ($e instanceof ShusyErrandOrder) {
    $e->delete();
  }
}

